package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.FollowUpRecord;
import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.mapper.FollowUpRecordMapper;
import com.ruoyi.business.mapper.PotentialCustomerMapper;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 潜在客户Service业务层处理
 *
 * @author x1ao
 * @date 2021-07-31
 */
@Service
@Transactional
public class PotentialCustomerServiceImpl extends ServiceImpl<PotentialCustomerMapper, PotentialCustomer> implements IPotentialCustomerService {
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;

    @Autowired
    private FollowUpRecordMapper followUpRecordMapper;

    /**
     * 查询潜在客户
     *
     * @param id 潜在客户ID
     * @return 潜在客户
     */
    @Override
    public PotentialCustomer selectPotentialCustomerById(Long id) {
        //return potentialCustomerMapper.selectPotentialCustomerById(id);
        return potentialCustomerMapper.selectById(id);
    }

    /**
     * 查询潜在客户列表
     *
     * @param potentialCustomer 潜在客户
     * @return 潜在客户
     */
    @Override
    public List<PotentialCustomer> selectPotentialCustomerList(PotentialCustomer potentialCustomer) {

        if (SecurityUtils.getSubject().hasRole("admin")) {
            LambdaQueryWrapper<FollowUpRecord> wrapper = new LambdaQueryWrapper<>();
            return potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
        }else {
            LambdaQueryWrapper<PotentialCustomer> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PotentialCustomer::getFollowUpUser, ShiroUtils.getUserId());
            return potentialCustomerMapper.selectList(wrapper);
        }
    }

    /**
     * 新增潜在客户
     *
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int insertPotentialCustomer(PotentialCustomer potentialCustomer) {
        LambdaQueryWrapper<PotentialCustomer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PotentialCustomer::getPhone, potentialCustomer.getPhone());
        Integer count = potentialCustomerMapper.selectCount(wrapper);
        System.out.println("count = " + count);
        if (count > 0) {
            return 0;
        } else {
           potentialCustomer.setCreateTime(DateUtils.getNowDate());
           return potentialCustomerMapper.insert(potentialCustomer);
        }
    }

    /**
     * 修改潜在客户
     *
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int updatePotentialCustomer(PotentialCustomer potentialCustomer) {
        //return potentialCustomerMapper.updatePotentialCustomer(potentialCustomer);
        //LambdaUpdateWrapper<PotentialCustomer> updateWrapper = new LambdaUpdateWrapper<>();
        //updateWrapper.eq(PotentialCustomer::getId,potentialCustomer.getId());
        return potentialCustomerMapper.updateById(potentialCustomer);
    }

    /**
     * 删除潜在客户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerByIds(String ids) {
        return potentialCustomerMapper.deletePotentialCustomerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除潜在客户信息
     *
     * @param id 潜在客户ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerById(Long id) {
        return potentialCustomerMapper.deletePotentialCustomerById(id);
    }
}
